2020-2021 Sunseeker Telemetry and Lighting System
timer_a_ex4_periodCapture.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx Timer_A - Period Capture
34  *
35  * Description: In this example, the 16 bit Timer_A is configured to capture
36  * the period of an incoming signal. For this example ACLK is looped back to
37  * the Timer_A capture pin to provide an example source. When the input clock
38  * signal goes from low to high the capture interrupt fires and the current
39  * value of the timer is stored in an array for data logging. To determine the
40  * period of the clock source simply subtract two adjacent capture values to
41  * get the number of ticks between their rising edges.
42  *
43  * In our example the input clock source is ACLK running at 32kHz. Timer_A is
44  * using SMCLK for sampling the data which runs at 16.384MHz or 61.04ns. The
45  * difference between samples is 512 meaning the input clock source has a
46  * period of 512 * 61.04ns = 31.25us or 32kHz. Once enough data captures have
47  * occurred the device blinks the LED indicating the data is ready.
48  *
49  * MSP430i2041
50  * ------------------
51  * /|\| |
52  * | | P1.4|--->LED
53  * --|RST |
54  * | P1.5/CC1A|<---|
55  * | | |
56  * | P1.2/ACLK|--->|
57  * | |
58  *
59  *
60  * Author: Zack Lalanne
61  ******************************************************************************/
62 #include "driverlib.h"
63 
64 #define NUMBER_OF_CAPTURES 50
65 
67 uint16_t CaptureDataIndex = 0;
68 
69 int main(void) {
70 
71  Timer_A_initCaptureModeParam captureModeConfig = {
72  TIMER_A_CAPTURECOMPARE_REGISTER_1, // CC Register 1
73  TIMER_A_CAPTUREMODE_RISING_EDGE, // Rising Edge
74  TIMER_A_CAPTURE_INPUTSELECT_CCIxA, // CCIxA Input Select
75  TIMER_A_CAPTURE_SYNCHRONOUS, // Synchronized Capture
76  TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE, // Enable interrupt
77  TIMER_A_OUTPUTMODE_OUTBITVALUE // Output bit value
78  };
79 
80  Timer_A_initContinuousModeParam continuousModeConfig = {
81  TIMER_A_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
82  TIMER_A_CLOCKSOURCE_DIVIDER_1, // SMCLK = 16.384MHz
83  TIMER_A_TAIE_INTERRUPT_DISABLE, // Disable Overflow ISR
84  TIMER_A_DO_CLEAR // Clear Counter
85  };
86 
87 
88  WDT_hold(WDT_BASE);
89 
90  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
91  // SMCLK = DCO = 16.384MHz
92  // ACLK = 32kHz
93  CS_setupDCO(CS_INTERNAL_RESISTOR);
94  CS_initClockSignal(CS_SMCLK, CS_CLOCK_DIVIDER_1);
95 
96  // Set P1.2 to output ACLK
97  GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1,
98  GPIO_PIN2,
99  GPIO_SECONDARY_MODULE_FUNCTION);
100 
101  // Set P1.5 to perform period measurement as CC1A
102  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
103  GPIO_PIN5,
104  GPIO_SECONDARY_MODULE_FUNCTION);
105 
106 
107  // Configure the timer to capture on rising edge of CC1A (P1.5)
108  Timer_A_initCaptureMode(TIMER_A0_BASE, &captureModeConfig);
109 
110  // Configure the sampling timer
111  Timer_A_initContinuousMode(TIMER_A0_BASE, &continuousModeConfig);
112  Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_CONTINUOUS_MODE);
113 
114  // Go into LPM0 with interrupts enabled. Don't exit until samples are done
115  __bis_SR_register(LPM0_bits | GIE);
116 
117  // All samples recorded, blink LED to indicate done
118  GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
119 
120  // Slow down clock so we can see LED blink
121  // Configure MCLK = ~1MHz
122  CS_initClockSignal(CS_MCLK, CS_CLOCK_DIVIDER_16);
123 
124  while(1) {
125  // Toggle the LED state
126  GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN4);
127  __delay_cycles(100000);
128  }
129 }
130 
131 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
132 #pragma vector=TIMER0_A1_VECTOR
133 __interrupt
134 #elif defined(__GNUC__)
135 __attribute__((interrupt(TIMER0_A1_VECTOR)))
136 #endif
137 void TA0_ISR(void) {
138  switch(__even_in_range(TAIV, TA0IV_TAIFG)) {
139  case TA0IV_NONE: break;
140  case TA0IV_TACCR1:
141  CaptureData[CaptureDataIndex++] = Timer_A_getCaptureCompareCount(TIMER_A0_BASE,
142  TIMER_A_CAPTURECOMPARE_REGISTER_1);
144  // Enough samples, can disable interrupt
145  Timer_A_disableCaptureCompareInterrupt(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1);
146 
147  // Exit from LPM0 and disable interrupts
148  __bic_SR_register_on_exit(LPM0_bits | GIE);
149  }
150  break;
151  case TA0IV_TACCR2: break;
152  case TA0IV_TAIFG: break;
153  default: break;
154  }
155 }
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)
#define NUMBER_OF_CAPTURES
uint16_t CaptureDataIndex
void TA0_ISR(void)
uint16_t CaptureData[NUMBER_OF_CAPTURES]
int main(void)